/*
 * Copyright 2008 Les Hazlewood
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pojodm.party;

import org.pojodm.entity.Entity;

import java.util.regex.Pattern;

/**
 * @author Les Hazlewood
 */
public class PostalAddress extends Entity {

    /** Valid regexp pattern for United States zip codes. */
    public static final Pattern US_VALID_ZIP_PATTERN = Pattern.compile("(\\d{5})(\\-\\d{4})?");

    private String line1;
    private String line2;
    private String line3;
    private String line4;
    private String city;
    private String county;
    //private Territory state;
    private String zip;
    private String zipExtension;
    //private Territory country;
    private String label;

    public PostalAddress() {}

    public String getLine1() {
        return line1;
    }

    public void setLine1( String line1 ) {
        this.line1 = line1;
    }

    public String getLine2() {
        return line2;
    }

    public void setLine2( String line2 ) {
        this.line2 = line2;
    }

    public String getLine3() {
        return line3;
    }

    public void setLine3( String line3 ) {
        this.line3 = line3;
    }

    public String getLine4() {
        return line4;
    }

    public void setLine4( String line4 ) {
        this.line4 = line4;
    }

    public String getCity() {
        return city;
    }

    public void setCity( String city ) {
        this.city = city;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty( String county ) {
        this.county = county;
    }

    /*public Territory getState() {
        return state;
    }

    public void setState( Territory state ) {
        this.state = state;
        if ( state != null ) {
            setStateString( state.getName() );
        }
    }*/

    public String getZip() {
        return zip;
    }

    public void setZip( String zip ) {
        this.zip = zip;
    }

    public String getZipExtension() {
        return zipExtension;
    }

    public void setZipExtension( String zipExtension ) {
        this.zipExtension = zipExtension;
    }

    /*public Territory getCountry() {
        return country;
    }

    public void setCountry( Territory country ) {
        this.country = country;
    }*/

    public String getLabel() {
        return label;
    }

    public void setLabel( String label ) {
        this.label = label;
    }

    public StringBuffer toStringBuffer() {
        StringBuffer sb = super.toStringBuffer();

        sb.append(",line1=").append(getLine1());
        if ( getLine2() != null ) { sb.append( ",line2=" ).append( getLine2() ); }
        if ( getLine3() != null ) { sb.append( ",line3=" ).append( getLine3() ); }
        if ( getLine4() != null ) { sb.append( ",line4=" ).append( getLine4() ); }
        sb.append(",city=").append( getCity() );
        if ( getCounty() != null ) { sb.append(",county=").append( getCounty() ); }
        //if ( getState() != null ) { sb.append( ",state=" ).append( getState() ); }
        sb.append(",zip=").append( getZip() );
        if ( getZipExtension() != null ) { sb.append(",zipExt=").append( getZipExtension() ); }
        //sb.append(",country=").append( getCountry() );
        if ( getLabel() != null ) { sb.append(",label=").append( getLabel() ); }

        return sb;
    }

    public boolean onEquals( Entity e ) {
        if ( e instanceof PostalAddress ) {

            final PostalAddress pa = (PostalAddress)e;

            return
                (line1.equals( pa.getLine1() ) ) &&
                (line2 == null ? pa.getLine2() == null : line2.equals( pa.getLine2() ) ) &&
                (line3 == null ? pa.getLine3() == null : line3.equals( pa.getLine3() ) ) &&
                (line4 == null ? pa.getLine4() == null : line4.equals( pa.getLine4() ) ) &&
                (city.equals( pa.getCity() ) ) &&
                (county == null ? pa.getCounty() == null : county.equals( pa.getCounty() ) ) &&
                //(state == null ? pa.getState() == null : state.equals( pa.getState() ) ) &&
                (zip.equals( pa.getZip() ) ) &&
                (zipExtension == null ? pa.getZipExtension() == null : zipExtension.equals( pa.getZipExtension() ) ) &&
                //(country.equals( pa.getCountry() ) ) &&
                (label == null ? pa.getLabel() == null : label.equals( pa.getLabel() ) );
        }

        return false;
    }

    public int hashCode() {
        int result = line1 != null ? line1.hashCode() : 0;
        result = 31 * result + ( line2 != null ? line2.hashCode() : 0 );
        result = 31 * result + ( line3 != null ? line3.hashCode() : 0 );
        result = 31 * result + ( line4 != null ? line4.hashCode() : 0 );
        result = 31 * result + ( city != null ? city.hashCode() : 0 );
        result = 31 * result + ( county != null ? county.hashCode() : 0 );
        //result = 31 * result + ( state != null ? state.hashCode() : 0 );
        result = 31 * result + ( zip != null ? zip.hashCode() : 0 );
        result = 31 * result + ( zipExtension != null ? zipExtension.hashCode() : 0 );
        //result = 31 * result + ( country != null ? country.hashCode() : 0 );
        result = 31 * result + ( label != null ? label.hashCode() : 0 );
        return result;
    }

    @Override
    @SuppressWarnings( { "CloneDoesntDeclareCloneNotSupportedException" } )
    public Object clone() {
        PostalAddress clone = (PostalAddress)super.clone();
        clone.setLine1( getLine1() );
        clone.setLine2( getLine2() );
        clone.setLine3( getLine3() );
        clone.setLine4( getLine4() );
        clone.setCity( getCity() );
        clone.setCounty( getCounty() );
        //clone.setState( getState() );
        clone.setZip( getZip() );
        clone.setZipExtension( getZipExtension() );
        //clone.setCountry( getCountry() );
        clone.setLabel( getLabel() );
        return clone;
    }

}
